<?php

/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: function_image.php 7350 2008-05-12 09:36:04Z liguode $
*/

if (!defined('IN_UCHOME')) {
	exit ('Access Denied');
}

//生成缩略图
function makethumb($srcfile,$type="thumb") {
	global $_SGLOBAL;

	//判断文件是否存在
	if (!file_exists($srcfile)) {
		return '';
	}
	
	$set_arr = array (
			"small" => "_small",
			"middle" => "_middle",
			"big" => "_big",
// 			"thumb" => ""
	);
	$k = $type;
	if(!isset($set_arr[$k])){
		return '';
	}
	$v = $set_arr[$k];
	
	$dstfile = $srcfile . '.' . $k . '.jpg';
	include_once (S_ROOT . './api/data_setting' . $v . '.php');

	//缩略图大小
	$tow = intval($_SGLOBAL['setting']['thumbwidth']);
	$toh = intval($_SGLOBAL['setting']['thumbheight']);
	if ($tow < 60)
		$tow = 60;
	if ($toh < 60)
		$toh = 60;

	$make_max = 0;
	$maxtow = intval($_SGLOBAL['setting']['maxthumbwidth']);
	$maxtoh = intval($_SGLOBAL['setting']['maxthumbheight']);
	if ($maxtow >= 300 && $maxtoh >= 300) {
		$make_max = 1;
	}

	//获取图片信息
	$im = '';
	if ($data = getimagesize($srcfile)) {
		if ($data[2] == 1) {
			$make_max = 0; //gif不处理
			if (function_exists("imagecreatefromgif")) {
				$im = imagecreatefromgif($srcfile);
			}
		}
		elseif ($data[2] == 2) {
			if (function_exists("imagecreatefromjpeg")) {
				$im = imagecreatefromjpeg($srcfile);
			}
		}
		elseif ($data[2] == 3) {
			if (function_exists("imagecreatefrompng")) {
				$im = imagecreatefrompng($srcfile);
			}
		}
	}
	if (!$im)
		return '';

	$srcw = imagesx($im);
	$srch = imagesy($im);

	$towh = $tow / $toh;
	$srcwh = $srcw / $srch;
	if ($towh <= $srcwh) {
		$ftow = $tow;
		$ftoh = $ftow * ($srch / $srcw);

		$fmaxtow = $maxtow;
		$fmaxtoh = $fmaxtow * ($srch / $srcw);
	} else {
		$ftoh = $toh;
		$ftow = $ftoh * ($srcw / $srch);

		$fmaxtoh = $maxtoh;
		$fmaxtow = $fmaxtoh * ($srcw / $srch);
	}
	if ($srcw <= $maxtow && $srch <= $maxtoh) {
		$make_max = 0; //不处理
	}
	if ($srcw > $tow || $srch > $toh) {
		if (function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @ $ni = imagecreatetruecolor($ftow, $ftoh)) {
			imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
			//大图片
			if ($make_max && @ $maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh)) {
				imagecopyresampled($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
			}
		}
		elseif (function_exists("imagecreate") && function_exists("imagecopyresized") && @ $ni = imagecreate($ftow, $ftoh)) {
			imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
			//大图片
			if ($make_max && @ $maxni = imagecreate($fmaxtow, $fmaxtoh)) {
				imagecopyresized($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
			}
		} else {
			return '';
		}
		if (function_exists('imagejpeg')) {
			imagejpeg($ni, $dstfile);
			//大图片
			if ($make_max) {
				imagejpeg($maxni, $srcfile);
			}
		}
		elseif (function_exists('imagepng')) {
			imagepng($ni, $dstfile);
			//大图片
			if ($make_max) {
				imagepng($maxni, $srcfile);
			}
		}
		imagedestroy($ni);
		if ($make_max) {
			imagedestroy($maxni);
		}
	}
	imagedestroy($im);

	if (!file_exists($dstfile)) {
		return '';
	} else {
		return $dstfile;
	}
}


function wallthumb($srcfile, $flag = 0) { //200X200 正方形缩略图
	global $_SGLOBAL, $_SC;
	
	if($flag){
		return wallthumb1($srcfile);
	}
	
	//判断文件是否存在
	if (!file_exists($srcfile)) {
		return '';
	}
	if (!is_dir($_SC[walldir] . "./thumbs")) {
		mkdir($_SC[walldir] . "./thumbs");
	}

	$dstfile = preg_replace('/(.*\/)([a-zA-Z0-9\._]+$)/', '\\1thumbs/\\2', $srcfile);
	include_once (S_ROOT . './3dwall/3dwall_settings.php');
	$imgName = $dstfile;

	//获取图片信息
	$im = '';
	if ($data = getimagesize($srcfile)) {
		if ($data[2] == 1) {
			$make_max = 0; //gif不处理
			if (function_exists("imagecreatefromgif")) {
				$im = imagecreatefromgif($srcfile);
			}
		}
		elseif ($data[2] == 2) {
			if (function_exists("imagecreatefromjpeg")) {
				$im = imagecreatefromjpeg($srcfile);
			}
		}
		elseif ($data[2] == 3) {
			if (function_exists("imagecreatefrompng")) {
				$im = imagecreatefrompng($srcfile);
			}
		}
	}

	$saveName = $imgName;
	

	if (file_exists($saveName)) {
		unlink($saveName);
	}
	$backgroundImg = $_SC['square_bg'];
	if (empty ($backgroundImg) || !is_file($backgroundImg) || !file_exists($backgroundImg)) {
		return false;
	}

	$imgWidth = imagesx($im);
	$imgHeight = imagesy($im);
	$targetImg = imagecreatefromjpeg($backgroundImg);

	$bgWidth = imagesx($targetImg);
	$bgHeight = imagesy($targetImg);

	$target_ratio = $bgWidth / $bgHeight;

	$img_ratio = $imgWidth / $imgHeight;

	if ($target_ratio > $img_ratio) {
		$new_height = $bgHeight;
		$new_width = $img_ratio * $bgHeight;
	} else {
		$new_height = $bgWidth / $img_ratio;
		$new_width = $bgWidth;
	}

	if ($new_height > $bgHeight) {
		$new_height = $bgHeight;
	}
	if ($new_width > $bgWidth) {
		$new_width = $bgWidth;
	}

	imagecopyresampled($targetImg, $im, ($bgWidth - $new_width) / 2, ($bgHeight - $new_height) / 2, 
						0, 0, $new_width, $new_height, $imgWidth, $imgHeight);

	imagejpeg($targetImg, $saveName);

//	file_put_contents("test/__.txt", $saveName);
	return $saveName;
}


//生成缩略图
function avatarthumb($srcfile) {
	global $_SGLOBAL, $_SC;

	//判断文件是否存在
	if (!file_exists($srcfile)) {
		return '';
	}	
	$dstfile = $srcfile;
	include_once (S_ROOT . './common/jcrop_settings.php');
	//缩略图大小
	$tow = intval($_SGLOBAL['setting']['thumbwidth']);
	$toh = intval($_SGLOBAL['setting']['thumbheight']);
	
	if ($tow < 60)
		$tow = 60;
	if ($toh < 60)
		$toh = 60;

	$make_max = 0;

//原图大小
//	$maxtow =  500;
//	$maxtoh = 1000;
	$maxtow = intval($_SGLOBAL['setting']['maxthumbwidth']);
	$maxtoh = intval($_SGLOBAL['setting']['maxthumbheight']);
//	file_put_contents("test/__.txt",$maxtow);
	if ($maxtow >= 300 && $maxtoh >= 300) {
		$make_max = 1;
	}

	//获取图片信息
	$im = '';
	if ($data = getimagesize($srcfile)) {
		if ($data[2] == 1) {
			$make_max = 0; //gif不处理
			if (function_exists("imagecreatefromgif")) {
				$im = imagecreatefromgif($srcfile);
			}
		}
		elseif ($data[2] == 2) {
			if (function_exists("imagecreatefromjpeg")) {
				$im = imagecreatefromjpeg($srcfile);
			}
		}
		elseif ($data[2] == 3) {
			if (function_exists("imagecreatefrompng")) {
				$im = imagecreatefrompng($srcfile);
			}
		}
	}
	if (!$im)
		return false;

	$srcw = imagesx($im);
	$srch = imagesy($im);

	$towh = $tow / $toh;
	$srcwh = $srcw / $srch;
	if ($towh <= $srcwh) {
		
		$fmaxtow = $maxtow;
		$fmaxtoh = $fmaxtow * ($srch / $srcw);
	} else {
		
		$fmaxtoh = $maxtoh;
		$fmaxtow = $fmaxtoh * ($srcw / $srch);
	}
	if ($srcw <= $maxtow && $srch <= $maxtoh) {
		$make_max = 0; //不处理
	}
	if ($srcw > $tow || $srch > $toh) {
		if (function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") &&$make_max && @ $maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh)) {
			//大图片
			imagecopyresampled($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
		}
		elseif (function_exists("imagecreate") && function_exists("imagecopyresized") && $make_max && @ $maxni = imagecreate($fmaxtow, $fmaxtoh)) {
			//大图片			
			imagecopyresized($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
		} else {
			return false;
		}
		if (function_exists('imagejpeg')) {
			//大图片
			if ($make_max) {
				imagejpeg($maxni, $srcfile);
			}
		}
		elseif (function_exists('imagepng')) {
			//大图片
			if ($make_max) {
				imagepng($maxni, $srcfile);
			}
		}


		if ($make_max) {
			imagedestroy($maxni);
		}
	}
	imagedestroy($im);
	
	return $srcfile;
}

//图片水印
function makewatermark($srcfile) {
	global $_SGLOBAL;

	include_once (S_ROOT . './api/data_setting.php');

	//水印图片
	$watermarkfile = empty ($_SGLOBAL['setting']['watermarkfile']) ? S_ROOT . './image/watermark.png' : $_SGLOBAL['setting']['watermarkfile'];
	if (!file_exists($watermarkfile) || !$water_info = getimagesize($watermarkfile)) {
		return '';
	}
	$water_w = $water_info[0];
	$water_h = $water_info[1];
	$water_im = '';
	switch ($water_info[2]) {
		case 1 :
			@ $water_im = imagecreatefromgif($watermarkfile);
			break;
		case 2 :
			@ $water_im = imagecreatefromjpeg($watermarkfile);
			break;
		case 3 :
			@ $water_im = imagecreatefrompng($watermarkfile);
			break;
		default :
			break;
	}
	if (empty ($water_im)) {
		return '';
	}

	//原图
	if (!file_exists($srcfile) || !$src_info = getimagesize($srcfile)) {
		return '';
	}
	$src_w = $src_info[0];
	$src_h = $src_info[1];
	$src_im = '';
	switch ($src_info[2]) {
		case 1 :
			//判断是否为动画
			$fp = fopen($srcfile, 'rb');
			$filecontent = fread($fp, filesize($srcfile));
			fclose($fp);
			if (strpos($filecontent, 'NETSCAPE2.0') === FALSE) { //动画图不加水印
				@ $src_im = imagecreatefromgif($srcfile);
			}
			break;
		case 2 :
			@ $src_im = imagecreatefromjpeg($srcfile);
			break;
		case 3 :
			@ $src_im = imagecreatefrompng($srcfile);
			break;
		default :
			break;
	}
	if (empty ($src_im)) {
		return '';
	}

	//加水印的图片的长度或宽度比水印小150px
	if (($src_w < $water_w +150) || ($src_h < $water_h +150)) {
		return '';
	}

	//位置
	switch ($_SGLOBAL['setting']['watermarkpos']) {
		case 1 : //顶端居左
			$posx = 0;
			$posy = 0;
			break;
		case 2 : //顶端居右
			$posx = $src_w - $water_w;
			$posy = 0;
			break;
		case 3 : //底端居左
			$posx = 0;
			$posy = $src_h - $water_h;
			break;
		case 4 : //底端居右
			$posx = $src_w - $water_w;
			$posy = $src_h - $water_h;
			break;
		default : //随机
			$posx = mt_rand(0, ($src_w - $water_w));
			$posy = mt_rand(0, ($src_h - $water_h));
			break;
	}

	//设定图像的混色模式
	@ imagealphablending($src_im, true);
	//拷贝水印到目标文件
	@ imagecopy($src_im, $water_im, $posx, $posy, 0, 0, $water_w, $water_h);
	switch ($src_info[2]) {
		case 1 :
			@ imagegif($src_im, $srcfile);
			break;
		case 2 :
			@ imagejpeg($src_im, $srcfile);
			break;
		case 3 :
			@ imagepng($src_im, $srcfile);
			break;
		default :
			return '';
	}
	@ imagedestroy($water_im);
	@ imagedestroy($src_im);
}



function createSquareImg($im, $imgName) { //200X200 正方形缩略图 图片裁剪
	global $_SGLOBAL, $_SC;

	$saveName = $imgName . ".square.jpg";

	if (file_exists($saveName)) {
		unlink($saveName);
	}
	$backgroundImg = $_SC['square_bg'];
	if (empty ($backgroundImg) || !is_file($backgroundImg) || !file_exists($backgroundImg)) {
		return false;
	}

	$imgWidth = imagesx($im);
	$imgHeight = imagesy($im);
	$targetImg = imagecreatefromjpeg($backgroundImg);

	$bgWidth = imagesx($targetImg);
	$bgHeight = imagesy($targetImg);

	$left = intval(($bgWidth - $imgWidth) / 2);
	$top = intval(($bgHeight - $imgHeight) / 2);

	//	echo $left."_".$top;die();
	imagecopymerge($targetImg, $im, $left, $top, 0, 0, $imgWidth, $imgHeight, 100);

	imagejpeg($targetImg, $saveName);

	return $saveName;
}

function createSquareImg1($im, $imgName) { //200X200 正方形缩略图 图片自动缩小 以适应缩略图
	global $_SGLOBAL, $_SC;

	$saveName = $imgName . ".square.jpg";


	if (file_exists($saveName)) {
		unlink($saveName);
	}
	$backgroundImg = $_SC['square_bg'];
	if (empty ($backgroundImg) || !is_file($backgroundImg) || !file_exists($backgroundImg)) {
		return false;
	}
	$imgWidth = imagesx($im);
	$imgHeight = imagesy($im);
	$targetImg = imagecreatefromjpeg($backgroundImg);

	$bgWidth = imagesx($targetImg);
	$bgHeight = imagesy($targetImg);

	$target_ratio = $bgWidth / $bgHeight;

	$img_ratio = $imgWidth / $imgHeight;

	if ($target_ratio > $img_ratio) {
		$new_height = $bgHeight;
		$new_width = $img_ratio * $bgHeight;
	} else {
		$new_height = $bgWidth / $img_ratio;
		$new_width = $bgWidth;
	}

	if ($new_height > $bgHeight) {
		$new_height = $bgHeight;
	}
	if ($new_width > $bgWidth) {
		$new_width = $bgWidth;
	}

	imagecopyresampled($targetImg, $im, ($bgWidth - $new_width) / 2, ($bgHeight - $new_height) / 2,
			0, 0, $new_width, $new_height, $imgWidth, $imgHeight);

	imagejpeg($targetImg, $saveName);

	return $saveName;
}

/**
 * 缩略图
 *
 * @param $path 图片路径
 * @param $name 图片名称
 * @param $width 图片宽度
 * @param $height 图片高度
 * @param $bgcolor 图片背景颜色
 *        	@判断长和宽的长度，计算比例
 */
 function mkThumb($res_path, $width = 100, $height = 100, $bgcolor = "FFFFFF") {
	$org_info = @getimagesize ( $res_path );

	switch ($org_info [2]) {
		case 1 :
		case 'image/gif' :
			$res = imagecreatefromgif ( $res_path );
			break;
				
		case 2 :
		case 'image/pjpeg' :
		case 'image/jpeg' :
		case 'image/jpg' :
			$res = imagecreatefromjpeg ( $res_path );
			break;
				
		case 3 :
		case 'image/x-png' :
		case 'image/png' :
			$res = imagecreatefrompng ( $res_path );
			break;
				
		default :
			return false;
	}

	$img_org = $res;

	/*
	 * 原始图片以及缩略图的尺寸比例
	*/
	$scale_org = $org_info [0] / $org_info [1];
	$img_thumb = imagecreatetruecolor ( $width, $height );
	$red = $green = $blue = "";
	sscanf ( $bgcolor, "%2x%2x%2x", $red, $green, $blue );
	$clr = imagecolorallocate ( $img_thumb, $red, $green, $blue );
	imagefilledrectangle ( $img_thumb, 0, 0, $width, $height, $clr );
	if ($org_info [0] / $width > $org_info [1] / $height) {
		$lessen_width = $width;
		$lessen_height = $width / $scale_org;
	} else {
		/*
		 * 原始图片比较高，则以高度为准
		*/
		$lessen_width = $height * $scale_org;
		$lessen_height = $height;
	}
	$dst_x = ($width - $lessen_width) / 2;
	$dst_y = ($height - $lessen_height) / 2;
	@imagecopyresampled ( $img_thumb, $img_org, $dst_x, $dst_y, 0, 0, $lessen_width, $lessen_height, $org_info [0], $org_info [1] );

	$thumb = "";
	if (function_exists ( 'imagejpeg' )) {
		$thumb .= '.thumb.jpg';
		imagejpeg ( $img_thumb, $res_path . $thumb, 100 );
	} elseif (function_exists ( 'imagegif' )) {
		$thumb .= '.thumb.gif';
		imagegif ( $img_thumb, $res_path . $thumb );
	} elseif (function_exists ( 'imagepng' )) {
		$thumb .= '.thumb.png';
		imagepng ( $img_thumb, $res_path . $thumb );
	}
	imagedestroy ( $img_thumb );
	imagedestroy ( $img_org );

	return array (
			"x" => $dst_x,
			"y" => $dst_y,
			"thumb" => $thumb
	);
}

//生成缩略图
function squareThumb($srcfile) {
	global $_SGLOBAL, $_SC;

	//判断文件是否存在
	if (!file_exists($srcfile)) {
		return '';
	}
	if (!is_dir($_SC[walldir] . "./thumbs")) {
		mkdir($_SC[walldir] . "./thumbs");
	}
	$dstfile = preg_replace('/(.*\/)([a-zA-Z0-9\._]+$)/', '\\1thumbs/\\2', $srcfile);
	include_once (S_ROOT . './3dwall/3dwall_settings.php');

	//缩略图大小
	$tow = intval($_SGLOBAL['setting']['thumbwidth']);
	$toh = intval($_SGLOBAL['setting']['thumbheight']);
	if ($tow < 60)
		$tow = 60;
	if ($toh < 60)
		$toh = 60;

	$make_max = 0;
	$maxtow = intval($_SGLOBAL['setting']['maxthumbwidth']);
	$maxtoh = intval($_SGLOBAL['setting']['maxthumbheight']);
	if ($maxtow >= 300 && $maxtoh >= 300) {
		$make_max = 1;
	}

	//获取图片信息
	$im = '';
	if ($data = getimagesize($srcfile)) {
		if ($data[2] == 1) {
			$make_max = 0; //gif不处理
			if (function_exists("imagecreatefromgif")) {
				$im = imagecreatefromgif($srcfile);
			}
		}
		elseif ($data[2] == 2) {
			if (function_exists("imagecreatefromjpeg")) {
				$im = imagecreatefromjpeg($srcfile);
			}
		}
		elseif ($data[2] == 3) {
			if (function_exists("imagecreatefrompng")) {
				$im = imagecreatefrompng($srcfile);
			}
		}
	}
	if (!$im)
		return '';

	$srcw = imagesx($im);
	$srch = imagesy($im);

	$towh = $tow / $toh;
	$srcwh = $srcw / $srch;
	if ($towh <= $srcwh) {
		$ftow = $tow;
		$ftoh = $ftow * ($srch / $srcw);

		$fmaxtow = $maxtow;
		$fmaxtoh = $fmaxtow * ($srch / $srcw);
	} else {
		$ftoh = $toh;
		$ftow = $ftoh * ($srcw / $srch);

		$fmaxtoh = $maxtoh;
		$fmaxtow = $fmaxtoh * ($srcw / $srch);
	}
	if ($srcw <= $maxtow && $srch <= $maxtoh) {
		$make_max = 0; //不处理
	}
	if ($srcw > $tow || $srch > $toh) {
		if (function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @ $ni = imagecreatetruecolor($ftow, $ftoh)) {
			imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
			//大图片
			if ($make_max && @ $maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh)) {
				imagecopyresampled($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
			}
		}
		elseif (function_exists("imagecreate") && function_exists("imagecopyresized") && @ $ni = imagecreate($ftow, $ftoh)) {
			imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
			//大图片
			if ($make_max && @ $maxni = imagecreate($fmaxtow, $fmaxtoh)) {
				imagecopyresized($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
			}
		} else {
			return '';
		}
		if (function_exists('imagejpeg')) {
			imagejpeg($ni, $dstfile);
			//大图片
			if ($make_max) {
				imagejpeg($maxni, $srcfile);
			}
		}
		elseif (function_exists('imagepng')) {
			imagepng($ni, $dstfile);
			//大图片
			if ($make_max) {
				imagepng($maxni, $srcfile);
			}
		}

		$squareImg = createSquareImg1($ni, $dstfile); //正方形缩略图
		imagedestroy($ni);

		if ($make_max) {
			imagedestroy($maxni);
		}
	}
	imagedestroy($im);

	if (!file_exists($dstfile)) {
		return '';
	} else {
		return $dstfile;
	}
}
?>